package com.pray.praymall.order.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * do something
 *
 * @author 魂悸魄
 * @date 2020/9/26 10:37
 */
@Configuration
public class PraymallFeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            // RequestContextHolder拿到刚进来的请求
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();// 老请求
                // 同步请求头数据
                template.header("Cookie", request.getHeader("Cookie"));
            }
        };
    }
}
